# -*- coding: UTF-8 -*-
from flaskext.wtf import Form, TextField, DecimalField, IntegerField, RadioField,\
TextAreaField, PasswordField, HiddenField, Required, validators, SelectField, DateTimeField
from wtforms.validators import ValidationError
from wtforms.fields import Field, DummyTranslations
from wtforms import widgets
import decimal
import time, datetime
import re

def username_not_null(form, field):
    if field.data is not None:
        field.data = field.data.strip()
    if field.data == None or len(field.data) < 4:
        raise ValidationError(u'用户名至少是4个字符')
    if re.search(r'[^\x00-\xff]',field.data) is not None:
        raise ValidationError(u'只允许是字母、下划线或数字，请检查是否打开输入法')

def password_not_null(form, field):
    if field.data == None or len(field.data) < 4 or len(field.data) > 16:
        raise ValidationError(u'密码长度在4－16之间')

def email_format_nullable(form, field):
    if field.data == None or len(field.data) == 0 or len(field.data) > 32:
        raise ValidationError(u'请输入email')
    if re.search(r'\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*',field.data) is None:
        raise ValidationError(u'email格式错误')

def name_not_null(form, field):
    if field.data is not None:
        field.data = field.data.strip()
    if field.data == None or len(field.data) < 2 or len(field.data) > 16:
        raise ValidationError(u'姓名长度在2-16之间')

class LoginForm(Form):
    username = TextField(u'用户名', validators=[username_not_null])
    password = PasswordField(u'密码', validators=[password_not_null])

class RegisterForm(Form):
    email = TextField(u'邮箱', validators=[email_format_nullable])
    password = PasswordField(u'密码', validators=[password_not_null])
    rpassword = PasswordField(u'再次输入密码', [validators.EqualTo('password', message=u'2次输入密码不一致')])
    name = TextField(u'姓名', validators=[name_not_null])

class ModifyPasswordForm(Form):
    password = PasswordField(u'原密码', validators=[password_not_null])
    npassword = PasswordField(u'新密码', validators=[password_not_null])
    rpassword = PasswordField(u'再次输入新密码', [validators.EqualTo('npassword', message=u'2次输入密码不一致')])

